package moc.zard.xg.controller;


import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import moc.zard.xg.mocEnum.status;
import moc.zard.xg.pojo.User;
import moc.zard.xg.result.Result;
import moc.zard.xg.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService IUserService;

    private User __user = new User().setUsername("hehe").setPassword("sb");

    @PostMapping("getUsername")
    public Result user_getUsername(String username) {
        Result result = Result.setOK();

        List<User> userListByUsername = null;
        userListByUsername = IUserService.getUserListByUsername(username);
        if (userListByUsername.size() > 0) {
            result.setData(Collections.singletonList(userListByUsername));
        } else {
            result.setCode(status.STATUS_ERROR.getCode());
            result.setMsg(status.STATUS_ERROR.getMsg());
            result.setData(null);
        }
        return result;
    }

    @PostMapping("register")
    public Result register(User user) {
        User user2 = new User();
        User _user = IUserService.insertUser(user);
        BeanUtils.copyProperties(_user,user2);
        Result result = Result.setOK();
        result.setData((Lists.newArrayList(user2)));
        return result;
    }

    @PostMapping("login")
    public Result login(String username, String password) {
        Result result = Result.setOK();
        User login = IUserService.login(username, password);
        if (login!=null){
            result.setData(Lists.newArrayList(login));
            return result;
        } else {
            return result;
        }
    }
}
